home *** CD-ROM | disk | FTP | other *** search
- // MacSerialPort.c
- // MacTech's Symantec Top Ten, November 1996
- //
- // This code sample walks through the basics of sending and receiving
- // character data from a serial port
- //
- // Thanks to Mark Y. Geschelin for the original code this is based on.
- //
-
- #include <console.h>
- #include <Serial.h>
- #include <Devices.h>
- #include <stdio.h>
- #include <console.h>
- #include <stdlib.h>
- #include <Serial.h>
- #include <Devices.h>
-
- #define SERBUFSIZ 1024 // Define the Input buffer size to use
-
- char *inbuf; // pointer to input character buffer
- short inRefNum, outRefNum; // Device driver Reference Number holders
-
- /////////////////////////////////////////
- // Initialize the Serial Port //
- /////////////////////////////////////////
- OSErr InitializeSerialPort()
- {
- OSErr err;
- SerShk flags;
- Ptr buf;
-
- // Open Serial Drivers (note: Use ".BIn" and ".BOut" for Printer port)
- // assign Output and Input driver reference numbers
- if (err = OpenDriver("\p.AOut",&outRefNum)) return err;
- if (err = OpenDriver("\p.AIn", &inRefNum)) return err;
-
- // Initialize input and output drivers, and
- // assign basic communication protocols
- if (err = SerReset(outRefNum,baud57600 + data8 + stop10+noParity))
- return err;
- if (err = SerReset(inRefNum,baud57600 + data8 + stop10+noParity))
- return err;
-
- // Set up the serial input driver to use a buffer of size SERBUFSIZ
- if(!(buf = NewPtr(SERBUFSIZ))) return MemError();
- if (err = SerSetBuf(inRefNum, buf, SERBUFSIZ)) return err;
-
- // Specify handshaking and cotrol info for the input driver
- flags.fXOn = false; // XOn/Xoff Output enabled?
- flags.fCTS = true; // Using Clear To Send harware handshaking?
- flags.xOn = 0x11; // Character for XOn
- flags.xOff = 0x13; // Character for XOff
- flags.errs = false; // Abort Input requests if: Parity error
- // or: Hardware overrun
- // or: Franing error
- flags.evts = false; // Post event on CTS or Break status change
- flags.fInX = false; // XOn/Xoff Input enabled?
- flags.fDTR = false; // Using Data Terminal Ready flow control
-
- // Set driver to reflect settings
- if ( err = SerHShake(outRefNum,&flags)) return err;
- // Allocate input buffer; return reason on failure
- if (!( inbuf = (char *) NewPtr(SERBUFSIZ))) return MemError();
-
- return noErr; // noErr = 0
- }
-
- ////////////////////////////////////////////////////////
- // Send a String to the Serial Port //
- ////////////////////////////////////////////////////////
- void SendSerial(char *outString, long strLen)
- {
- FSWrite(outRefNum, &strLen, outString);
- }
-
- // Thanks to Andrew McFarland, Noah Lieberman and Levi Brown for their contributions.
-
-
- /////////////////////////////
- // Main //
- /////////////////////////////
- int main(void)
- {
- OSErr err;
- long count;
- char keyChar;
-
- csetmode(C_RAW,stdin); // disable echo and line buffering for input
-
- if (err = InitializeSerialPort()) // Check for failure to initialize port
- printf("Serial initialization failed. Error = %d\n",err);
- else
- {
- SendSerial("ATX\r",5); // Send ubiquitous Hayes reset
-
- keyChar = getchar(); // Get a character from stdin
- while (keyChar != 0x1B) // Loop until escape key is pressed
- {
- if (keyChar > 0) // Is there a character to send?
- SendSerial(&keyChar,1); // Call SendSerial to send it.
-
- SerGetBuf(inRefNum, &count);
- // Is there anything in the Input buffer
- if (count)
- {
- FSRead(inRefNum,&count,inbuf);
- // Read all chars from Input driver
- // Send to console
- for (long i=0; i < count; putchar(inbuf[i++]));
- }
- keyChar = getchar(); // Get another character from stdin
- }
- }
-
- // Clean up: Reset Ports, return pointer
- if(outRefNum) CloseDriver(outRefNum);
- if(inRefNum) CloseDriver(inRefNum);
- if (inbuf) DisposPtr(inbuf);
-
- return EXIT_SUCCESS;
- }
-